﻿using HZY.Framework.DependencyInjection;
using Long.DAL;
using Long.Helper;
using Long.Model;
using Mapster;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Long.BLL
{
    public class UserManager:IScopedSelfDependency
    {
        private readonly UserService _userService;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="userService"></param>
        public UserManager(UserService userService)
        {
            _userService = userService;
        }

        public async Task<BaseResult<UserEntity>> LoginAsyncManager(UserLoginDto request)
        {
            //将 request 映射成 UserEntity
            var entity = request.Adapt<UserEntity>();

           var res = await _userService.LoginAsyncService(entity);
            if(res.ResultStatus == SystemEnums.ResultStatus.Fail)
            {
                return new BaseResult<UserEntity>() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = res.Msg };
            }
            return res;
        }

        public async Task<BaseResult> IsUserNameExistAsyncManager(UserExistDto request)
        {
            var entity = request.Adapt<UserEntity>();
            bool res = await _userService.ExistAsync(e => e.UserName == entity.UserName);
            if (!res)
            {
                return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = "用户不存在" };
            }
            return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Success};
        }

        public async Task<BaseResult> AddUserAsyncManager(UserAddDto request)
        {
            BaseResult isExistRes = await IsUserNameExistAsyncManager(new UserExistDto() { UserName = request.UserName });
            if(isExistRes.ResultStatus == SystemEnums.ResultStatus.Success)
            {
                return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = "用户名已存在" };
            }

            var entity = request.Adapt<UserEntity>();
            int res = await _userService.InsertAsync(entity);
            if (res < 1)
            {
                return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = $"新增 {request.UserName} 失败" };
            }
            return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Success };
        }

        public async Task<BaseResult> UpdateUserAsyncManager(UserUpdateDto request)
        {
            //用户名改成已存在的用户名，不可取
            bool isExist = await _userService.ExistAsync(c => c.UserName == request.UserName && c.Id != request.Id);
            if (isExist)
            {
                return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = "用户名已存在" };
            }
            var entity = request.Adapt<UserEntity>();
            bool res = await _userService.UpdateAsync(entity);
            if (!res)
            {
                return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = $"更新 {request.UserName} 失败" };
            }
            return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Success };
        }

        public async Task<BaseResult> DeleteUserAsyncManager(UserDeleteDto request)
        {
            var entity = request.Adapt<UserEntity>();
            bool res = await _userService.DeleteAsync(entity);
            if (!res)
            {
                return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = $"删除失败" };
            }
            return new BaseResult() { ResultStatus = SystemEnums.ResultStatus.Success };
        }

        public async Task<BaseResult<UserQueryResultDto>> GetUserListAsyncManager()
        {
            List<UserEntity> res = await _userService.GetByListAsync(c=>true);

            List<UserQueryResultDto> dtos = res.Adapt<List<UserQueryResultDto>>();
            return new BaseResult<UserQueryResultDto>() { ResultStatus = SystemEnums.ResultStatus.Success, Data = dtos };
        }

        /// <summary>
        /// 根据用户名查询用户权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<BaseResult<UserQueryResultDto>> GetUserAuthAsyncManager(UserAuthQueryDto request)
        {
            BaseResult isExist = await IsUserNameExistAsyncManager(new UserExistDto{ UserName = request.UserName });
            if(isExist.ResultStatus == SystemEnums.ResultStatus.Fail)
            { //不存在
                new BaseResult<UserQueryResultDto>() { ResultStatus = SystemEnums.ResultStatus.Fail, Msg = "用户不存在" };
            }

            UserEntity res = await _userService.GetByOneAsync(c=>c.UserName == request.UserName);
            return new BaseResult<UserQueryResultDto>()
            {
                ResultStatus = SystemEnums.ResultStatus.Success,
                Data = new List<UserQueryResultDto>() { res.Adapt<UserQueryResultDto>() }
            };
        }
    }
}
